package com.llly.controller;

import com.llly.client.ProductFeignClient;
import com.llly.client.ShoppingCarFeignClient;
import com.llly.pojo.Easybuy_product;
import com.llly.pojo.Easybuy_product_category;
import com.llly.pojo.Easybuy_user;
import com.llly.pojo.ShoppingCar;
import com.netflix.ribbon.proxy.annotation.Http;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
public class ProductController {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ShoppingCarFeignClient shoppingCarFeignClient;

    //根据商品id查找商品
    @RequestMapping("/findProductByPid")
    public ModelAndView findProductByPid(Integer pid,ModelAndView mv,HttpSession session){
        Easybuy_product productByPid = productFeignClient.findProductByPid(pid);
            System.out.println(productByPid);
        mv.addObject("product",productByPid);
        Easybuy_product_category epc = productFeignClient.findpname(pid);
        mv.addObject("epc",epc);
            System.out.println(epc.getPname());

        //商品分类部分
        List<Easybuy_product_category> allEpcs = productFeignClient.findAllEpcs();   //左侧分类栏及商品
        mv.addObject("epcs",allEpcs);

        //购物车部分
        Easybuy_user user = (Easybuy_user) session.getAttribute("user");
        Integer uid = user.getId();
        Map<String, Object> map = shoppingCarFeignClient.showAllShoppingCar(uid);
        if (map!=null){
            List<ShoppingCar> shoppingCars= (List<ShoppingCar>) map.get("cars");
            mv.addObject("snum",shoppingCars.size());
            mv.addObject("zj",map.get("zj"));
            mv.addObject("shopcars",shoppingCars);
        }else{
            mv.addObject("snum",0);
            mv.addObject("zj",0);
        }

        mv.setViewName("Product");
        return mv;
    }

    //根据商品分类id 查找对应商品
    @RequestMapping("/findAllprosByEpc")
    public ModelAndView findAllprosByEpc(Integer epcId,ModelAndView mv,HttpSession session){
        //商品分类部分
        List<Easybuy_product_category> allEpcs = productFeignClient.findAllEpcs();   //左侧分类栏及商品
        mv.addObject("epcs",allEpcs);

        //购物车部分
        Easybuy_user user = (Easybuy_user) session.getAttribute("user");
        Integer uid = user.getId();
        Map<String, Object> map = shoppingCarFeignClient.showAllShoppingCar(uid);
        if (map!=null){
            List<ShoppingCar> shoppingCars= (List<ShoppingCar>) map.get("cars");
            mv.addObject("snum",shoppingCars.size());
            mv.addObject("zj",map.get("zj"));
            mv.addObject("shopcars",shoppingCars);
        }else{
            mv.addObject("snum",0);
            mv.addObject("zj",0);
        }

        List<Easybuy_product> allprosByEpc = productFeignClient.findAllprosByEpc(epcId);
        mv.addObject("product",allprosByEpc);
        mv.setViewName("ProductList");
        return mv;
    }

    //点击左导航按分类id搜索分类下商品
    @RequestMapping("/selectEpsByEpcId")
    public ModelAndView selectEpsByEpcId(ModelAndView mv, Integer id,HttpSession session){
        System.out.println("8081的selectEpsByEpcId");
        List<Easybuy_product_category> allEpcs = productFeignClient.findAllEpcs();   //左侧分类栏及商品
        List<Easybuy_product> easybuy_products = productFeignClient.selectEpsByEpcId(id);

        //购物车显示
        Easybuy_user user = (Easybuy_user) session.getAttribute("user");
        Integer uid = user.getId();
        Map<String, Object> map = shoppingCarFeignClient.showAllShoppingCar(uid);
        if (map!=null){
            List<ShoppingCar> shoppingCars= (List<ShoppingCar>) map.get("cars");
            mv.addObject("snum",shoppingCars.size());
            mv.addObject("zj",map.get("zj"));
            mv.addObject("shopcars",shoppingCars);
        }else{
            mv.addObject("snum",0);
            mv.addObject("zj",0);
        }

        mv.addObject("prods",easybuy_products);
        mv.addObject("jijian2",easybuy_products.size());
        mv.addObject("epcs",allEpcs);   //1级分类
        mv.setViewName("ProductList");
        return mv;
    }


    //根据输入的查询商品
   @RequestMapping("/showProduct")
   public ModelAndView showProduct(ModelAndView mv, String epname,HttpSession session){
       System.out.println("进入showProduct方法*****************");

       Easybuy_user user = (Easybuy_user) session.getAttribute("user");
       Integer uid = user.getId();
       //购物车
       Map<String, Object> map = shoppingCarFeignClient.showAllShoppingCar(uid);
       if (map!=null){
           List<ShoppingCar> shoppingCars= (List<ShoppingCar>) map.get("cars");
           mv.addObject("snum",shoppingCars.size());
           mv.addObject("zj",map.get("zj"));
           mv.addObject("shopcars",shoppingCars);
       }else{
           mv.addObject("snum",0);
           mv.addObject("zj",0);
       }
       //商品分类部分
       List<Easybuy_product_category> allEpcs = productFeignClient.findAllEpcs();   //左侧分类栏及商品
       mv.addObject("epcs",allEpcs);

       List<Easybuy_product> easybuyProductList = productFeignClient.showProduct(epname);
       mv.addObject("showProduct",easybuyProductList);
       mv.addObject("jijian",easybuyProductList.size());
       mv.addObject("keywords",epname); //回显
       mv.setViewName("ProductList");
       return mv;
   };
    //显示一级分类
    @RequestMapping("ShowAllOne")
    public  ModelAndView ShowAllOne(ModelAndView mv){
        List<Easybuy_product_category> epc = productFeignClient.ShowAllOne();
        mv.addObject("epcOne",epc);
        mv.setViewName("Manager/product_sj");
        return mv;
    }
    //显示二级分类
    @RequestMapping("ShowAllTwo")
    public  List ShowAllTwo(ModelAndView mv,@RequestParam Integer parentId){
        System.out.printf("++++++++++++"+parentId);
        List<Easybuy_product_category> epc = productFeignClient.ShowAllTwo(parentId);
      return epc;
    }
    //显示三级分类
    @RequestMapping("ShowAllThree")
    public  List ShowAllThree(ModelAndView mv,@RequestParam Integer parentId){
        System.out.printf("++++++++++++"+parentId);
        List<Easybuy_product_category> epc = productFeignClient.ShowAllThree(parentId);
        return epc;
    }
    @RequestMapping("/addeasybuy")
    public ModelAndView addeasybuy(Easybuy_product easybuy_product,ModelAndView mv,@RequestParam("photo") MultipartFile photo){
        System.out.printf("传入的商品"+easybuy_product);
        String originalFilename = photo.getOriginalFilename();
        System.out.println("传入图片信息："+originalFilename);
        //产生随机数图片名称
        String s = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        //给实体类复制fileName 保存到数据库
        easybuy_product.setFileName(s+originalFilename);
        String path="D:/sources/nginx/nginx-1.16.1/html";//图片传ngix
        File file=new File(path,s+originalFilename);
        try {
            photo.transferTo(file);//写nginx图片文件夹
            System.out.println("最终传入数据库，商品对象："+easybuy_product);
            productFeignClient.addeasybuy(easybuy_product);


        } catch (IOException e) {
            e.printStackTrace();
        }
        mv.setViewName("Manager/index");
        return mv;
    }
}
